var common = require('../../../utils/common.js');
var app=getApp();
var baseUrl=app.url;

Page({
    data: {
        open_search:true,
        inputShowed: false,
        search_input: "",
        page:1,
        carList:{},
        searchLoading: false, 
        searchLoadingComplete: false ,  
        lable:[['车架号','物联网卡号','添加时间'],["car_unique_id","cardcode","create_time"],"car_id","car_sn"],
        filter:[],
        brand_id:"",
        relevance:"",
        agency_id:"",
        supplier_no:""
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    seachInput: function () {
        this.setData({carList:{},search_input:this.data.search_input,page:1});
        this.fetchSearchList(1,10);  

    },
    clearInput: function () {
        this.setData({
            search_input: "",
            inputShowed:false,
            carList:{}
        });
        this.fetchSearchList(1,10);
    },
    inputTyping: function (e) {
        this.setData({
            search_input: e.detail.value     
        });
        this.setData({carList:{}});
        this.fetchSearchList(1,10,this.data.search_input);
    },
    onLoad:function (options) {
        var type = options.type;
        if(type){
            this.setData({inputShowed:true})
        }else{
            this.setData({inputShowed:false})
        }
        this.setData({carList:{}});
        this.fetchSearchList(1,10); 
        var relevance ={"relevance":[{"id":1,"name":"分时租赁"},{"id":2,"name":"企业用车"},{"id":3,"name":"共享用车"},{"id":4,"name":"长租车辆"},{"id":5,"name":"自测试用"},{"id":6,"name":"上牌整备"},{"id":7,"name":"整备待租"}]};
        var supplier_no = {"supplier_no":[{"id":1376920002,"name":"赛卡(博鹭途)"},{"id":1376920001,"name":"唯特迅"}]};
        common.bestCurl('GET','/cars/add',res=>{
            if(res.statusCode==200){
                this.setData({filter:Object.assign(res.data,relevance,supplier_no) });
            }   
        })   
    },  
    fetchSearchList: function(page,length){
        data = this.data;
        this.setData({searchLoading:true,searchLoadingComplete:false})
        common.bestCurl('POST','/cars/index',res=>{
            if(res.statusCode==200){
                if(res.data.code!=2){
                    if(res.data.data.length==0){
                        this.setData({searchLoadingComplete:true,searchLoading:false})
                    }else{
                        if (this.data.carList.length) {
                            setTimeout(e=>{
                            this.setData({ carList:  this.data.carList.concat(res.data.data)})         
                            // 	this.setData({searchLoading:false})         

                            },500)
                        } else {
                            this.setData({ carList:  res.data.data,searchLoading:false})
                        }                        
                    }
                }else{
                    wx.showToast({
                        title: '无法加载数据',
                        icon: 'loading',
                        duration: 15000
                    });
                }
            }            
        },Object.assign({search_input:data.search_input,relevance:data.relevance,brand_id:data.brand_id,agency_id:data.agency_id,supplier_no:data.supplier_no},{'page':page,'length':length}),'../../');
    },
    onReachBottom:function (){
        var page = this.data.page;
            page++;
        this.setData({page:page})
        this.fetchSearchList(page,10);
    }, 
    brand_id:function(event){
        var value = null;
        var id = event.currentTarget.id, list = this.data.filter;
        for (var i = 0, len = list.brand.length; i < len; ++i) {
            if (list.brand[i].id == id) {
                if(list.brand[i].open != true){
                    value = id;
                }
                list.brand[i].open = !list.brand[i].open
            } else {
                list.brand[i].open = false
            }
        }
        this.setData({filter:list,brand_id:value});
    },
    relevance:function(event){
        var value = null;
        var id = event.currentTarget.id, list = this.data.filter;
        for (var i = 0, len = list.relevance.length; i < len; ++i) {
            if (list.relevance[i].id == id) {
                if(list.relevance[i].open != true){
                    value = id;
                }
                list.relevance[i].open = !list.relevance[i].open
            } else {
                list.relevance[i].open = false
            }
        }
        this.setData({filter:list,relevance:value});
    },
    agency_id:function(event){
        var value = null;
        var id = event.currentTarget.id, list = this.data.filter;
        for (var i = 0, len = list.agency.length; i < len; ++i) {
            if (list.agency[i].id == id) {
                if(list.agency[i].open != true){
                    value = id;
                }
                list.agency[i].open = !list.agency[i].open
            } else {
                list.agency[i].open = false
            }
        }
        this.setData({filter:list,agency_id:value});
    },
    supplier_no:function(event){
        var value = null;
        var id = event.currentTarget.id, list = this.data.filter;
        for (var i = 0, len = list.supplier_no.length; i < len; ++i) {
            if (list.supplier_no[i].id == id) {
                if(list.supplier_no[i].open != true){
                    value = id;
                }
                list.supplier_no[i].open = !list.supplier_no[i].open
            } else {
                list.supplier_no[i].open = false
            }
        }
        this.setData({filter:list,supplier_no:value});
    },
    submit_search:function(){
        
        this.setData({carList:{},open_search:true,page:1});
        this.fetchSearchList(1,10);
    },
    reset_search:function() {
        var relevance ={"relevance":[{"id":1,"name":"分时租赁"},{"id":2,"name":"企业用车"},{"id":3,"name":"共享用车"},{"id":4,"name":"长租车辆"},{"id":5,"name":"自测试用"},{"id":6,"name":"上牌整备"},{"id":7,"name":"整备待租"}]};
        var supplier_no = {"supplier_no":[{"id":1376920002,"name":"赛卡(博鹭途)"},{"id":1376920001,"name":"唯特迅"}]};
        common.bestCurl('GET','/cars/add',res=>{
            if(res.statusCode==200){
                this.setData({filter:Object.assign(res.data,relevance,supplier_no), search_input:"",relevance:"",brand_id:"",agency_id:"",supplier_no:""});
            }
        })
    },
    filter:function() {
        this.setData({open_search:false})
    }
})
